package com.aramis.mydytt.ext

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.util.Log
import android.view.View
import android.view.WindowManager
import com.aramis.mydytt.ui.bean.MovieItemBean
import com.aramis.mydytt.ui.detail.MovieDetailActivity
import java.security.MessageDigest
import java.text.SimpleDateFormat
import java.util.*

/**
 * Description: common
 * @author lizhidan
 * Created: 2021/4/7
 */
private const val TAG = "Aramis"
const val TOKEN_DEFAULT = "a625824ed67face9e412cc4b41f3af0a"
const val KV_HISTORY_KEY = "search_history"

fun logE(msg: String?) {
    Log.e(TAG, msg ?: "null")
}

fun logD(msg: String?) {
    Log.d(TAG, msg ?: "null")
}

fun Context.dip(dp: Int): Int {
    if (density == 0f) {
        density = resources.displayMetrics.density
    }
    return if (dp > 0) (dp * density + 0.5f).toInt() else dp
}

fun Dialog.dip(dp: Int): Int = context.dip(dp)
fun View.dip(dp: Int): Int = context.dip(dp)

private var screenWidth = 0
private var screenHeight = 0
private var density = 0f
fun Context.screenWidth(): Int {
    return if (screenWidth == 0) {
        screenWidth = this.resources.displayMetrics.widthPixels
        screenWidth
    } else {
        screenWidth
    }
}

fun Context.screenHeight(): Int {
    return if (screenHeight == 0) {
        screenHeight = this.resources.displayMetrics.heightPixels
        screenHeight
    } else {
        screenHeight
    }
}

fun String.md5(): String {
    val bytes = MessageDigest.getInstance("MD5").digest(this.toByteArray())
    return bytes.hex()
}

fun ByteArray.hex(): String {
    return joinToString("") { "%02X".format(it) }
}

fun getFriendlyTimeStr(time: String): String {
    if (time.isNotBlank()) {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
        val parseLong = simpleDateFormat.parse(time)?.time
        if (parseLong != null) {
            val current = System.currentTimeMillis()
            return when {
                current - parseLong < 60 * 1000 -> "几秒钟前"
                current - parseLong < 60 * 60 * 1000 -> {
                    "${((current - parseLong) / (60 * 1000)).toInt()}分钟前"
                }
                current - parseLong < 24 * 60 * 60 * 1000 -> {
                    "${((current - parseLong) / (60 * 60 * 1000)).toInt()}小时前"
                }
                current - parseLong < 7 * 24 * 60 * 60 * 1000 -> {
                    "${((current - parseLong) / (24 * 60 * 60 * 1000)).toInt()}天前"
                }
                current - parseLong < 5 * 7 * 24 * 60 * 60 * 1000L -> {
                    "${((current - parseLong) / (7 * 24 * 60 * 60 * 1000)).toInt()}周前"
                }
                current - parseLong < 12 * 4 * 7 * 24 * 60 * 60 * 1000L -> {
                    "${((current - parseLong) / (4 * 7 * 24 * 60 * 60 * 1000L)).toInt()}月前"
                }
                else -> {
                    "${((current - parseLong) / (12 * 4 * 7 * 24 * 60 * 60 * 1000L)).toInt()}年前"
                }
            }
        }
    }
    return time
}

fun Activity.commonStartMovieDetailListener(): (MovieItemBean) -> Unit {
    return {
        MovieDetailActivity.start(this, it)
    }
}